package com.lonty.intelligence.service;

import com.lonty.intelligence.mapper.MicroDynamicMapper;
import com.lonty.intelligence.model.MicroDynamic;
import com.lonty.intelligence.request.MicroDynamicList;
import com.lonty.intelligence.util.UUIDUtil;
import com.lonty.intelligence.vo.MicroDynamicVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author 陈璇
 * @Date 2020/7/7 10:46
 * @Version 1.0
 */
@Service
public class MicroDynamicService {

    @Resource
    private MicroDynamicMapper microDynamicMapper;

    /**
     * 保存微动态
     * @param microDynamicVo
     * @return
     */
    public boolean saveMicroDynamic(MicroDynamicVo microDynamicVo){
        MicroDynamic microDynamic = new MicroDynamic();
        BeanUtils.copyProperties(microDynamicVo, microDynamic);
        microDynamic.setId(UUIDUtil.randomID());
        microDynamic.setCreateTime(LocalDateTime.now());
        microDynamic.setIsShelf(1);
        microDynamic.setNum(0);
        return microDynamicMapper.saveMicroDynamic(microDynamic) > 0;
    }

    /**
     * 根据ID获取微动态
     * @param id
     * @return
     */
    public MicroDynamic getById(String id){
        return microDynamicMapper.getById(id);
    }

    /**
     * 分页获取微动态
     * @param page
     * @param size
     * @return
     */
    public MicroDynamicList getMicroDynamicList(Integer page, Integer size){
        List<MicroDynamic> microDynamicList = microDynamicMapper.getMicroDynamicList((page - 1) * size, size);
        return MicroDynamicList.builder()
                .microDynamicList(microDynamicList)
                .total(microDynamicMapper.countMicroDynamic())
                .page(page)
                .build();
    }

    /**
     * 删除微动态
     * @param id
     * @return
     */
    public boolean deleteMicroDynamic(String id){
        return microDynamicMapper.deleteMicroDynamic(id) > 0;
    }

    /**
     * 根据ID修改点击数量
     * @param id
     * @return
     */
    public boolean modifyNum(String id){
        MicroDynamic microDynamic = microDynamicMapper.getById(id);
        return microDynamicMapper.modifyNum(id, microDynamic.getNum() + 1) > 0;
    }

}
